function adjustColumnWidth(table,vnode) {
  const CN = new RegExp("[\u9FA5]|[^\uFF00-\uFFFF]")
  const NUM = new RegExp("[0-9]")
  const LATTER = new RegExp("[a-zA-Z][a-zA-Z]")
  const CN_RATE = 0.72
  const NUM_RATE = 0.32
  const OTHER_RATE = 0.2
  const LATTER_RATE = 0.2

  const columns = vnode.child.columns.slice()
  const colgroup = table.querySelector("colgroup")
  const colDefs = [...colgroup.querySelectorAll("col")]

  const gutter = colgroup.querySelector(`col[name=gutter]`)
  const gutterx = colgroup.querySelector(`col[name=gutterx]`)
  let except = 0
  if(gutter || gutterx) {
    colDefs.pop()
  }
  except = colDefs.length - columns.length
  colDefs.splice(0,except)
  for(let i = columns.length - 1;i >= 0;i--) {
    if(columns[i].width) {
      colDefs.splice(i, 1);
      columns.splice(i, 1);
    }
  }

  colDefs.forEach((col, index) => {
    const clsName = col.getAttribute('name')
    const cells = [
      ...table.querySelectorAll(`.el-table__body-wrapper td.${clsName}`),
      ...table.querySelectorAll(`th.${clsName}`)
    ];
    const widthList = cells.map((el) => {
      const cell = el.querySelector('.cell')
      if(cell) {
        let fontSize = parseInt(window.getComputedStyle(cell,null).fontSize)
        let existWidth = parseInt(window.getComputedStyle(cell,null).width)
        if(existWidth) return existWidth;
        fontSize = fontSize ? fontSize : 12
        let width = 0;
        for(let str of cell.innerText) {
          if(NUM.test(str)) {
            width += fontSize * NUM_RATE;
            continue;
          } else if(LATTER.test(str)) {
            width += fontSize * LATTER_RATE;
            continue;
          } else if(CN.test(str)) {
            width += fontSize * CN_RATE;
            continue;
          } else {
            width += fontSize * OTHER_RATE;
          }
        }

        return Math.ceil(width)
      } else {
        return 0
      }
    });
    let max = Math.max(...widthList);
    if(max) {
      if(max < 100) {
        max = 100
      }
      if(max > 300) max = 300
    }
    if(max !== 0) {
      columns[index].minWidth = max //+ 22;
      table.querySelectorAll(`col[name=${clsName}]`).forEach((el) => {
        el.setAttribute("width", max + 25)
      })
    }
  })
  vnode.child.doLayout()
  tableRevise(table)
}

function tableRevise(table) {
  const tableWrapper = table.querySelector('.el-table__body-wrapper');
  const tableBody = table.querySelector('.el-table__body');
  const colgroup = table.querySelector('colgroup')

  if(tableBody.clientWidth > tableWrapper.offsetWidth) {
    tableWrapper.style.overflowX = 'auto'
    let fixedWrap = table.querySelectorAll('.el-table .el-table__fixed-body-wrapper')
    if(fixedWrap.length > 0) {
      fixedWrap.forEach(item => {
        item.style.paddingBottom = 8 + 'px'
      })
    }

    let fixed_left = table.querySelector('.el-table .el-table__fixed')
    let fixed_right = table.querySelector('.el-table .el-table__fixed-right')
    if(fixed_left) {
      fixed_left.style.height = 'calc(100% - 8px)'
    }
    if(fixed_right) {
      fixed_right.style.height = 'calc(100% - 8px)'
    }

    const gutter = colgroup.querySelector(`col[name=gutter]`);
    const gutterx = colgroup.querySelector(`col[name=gutterx]`);
    if(!gutter && !gutterx) {
      let col = document.createElement('col');
      col.setAttribute('name','gutterx')
      col.setAttribute('width','8')
      colgroup.appendChild(col)
    }
  }
}

export default {
  inserted(el, binding, vnode) {
    setTimeout(() => {
      adjustColumnWidth(el,vnode);
    },300)
  },
  componentUpdated(el,binding,vnode) {
    el.classList.add('r-table');
    setTimeout(() => {
      adjustColumnWidth(el,vnode);
    },300)
  },
  unbind() {}
}
